﻿#include <iostream>
#include "ReadWriteMemoryR3.h"
#include "tlhelp32.h"
#include "HandleR3.h"
#include "resource.h"
#include <string>
//CReadWriteMemoryR3 CReadWrite;
CHandle h;
HINSTANCE hinst = NULL;
HWND hwndDLG = NULL;
using namespace std;

DWORD GetProcessIdFromName(const wchar_t* name)
{
    HANDLE  hsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hsnapshot == INVALID_HANDLE_VALUE)  return 0;
   
    PROCESSENTRY32 pe;
    pe.dwSize = sizeof(PROCESSENTRY32);

    int flag = Process32First(hsnapshot, &pe);

    while (flag != 0)
    {
		
        if (lstrcmpW(pe.szExeFile, name) == 0)
        {
            return pe.th32ProcessID;
        }
        flag = Process32Next(hsnapshot, &pe);
    }

    CloseHandle(hsnapshot);

    return 0;
}


//void farCall()
//{
//    DWORD pid = GetProcessIdFromName(L"R3.exe");
//
//    // HANDLE handle =  OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid);
//  
//
//    
//    HMODULE hmodule = LoadLibraryA("user32.dll");
//    ULONG_PTR msg = (ULONG_PTR)GetProcAddress(hmodule, "MessageBoxA");
//
//    //31 C9 31 D2 4D 31 C0 4D 31 C9 48 B8 99 78 56 34 12 00 00 00 48 81 EC A8 00 00 00 FF D0 48 81 C4 A8 00 00 00 C3
//    char bufcode[] =
//    {
//        0x31, 0xC9,
//        0x31, 0xD2,
//        0x4D, 0x31, 0xC0,
//        0x4D, 0x31, 0xC9,
//        0x48, 0xB8, 0x99, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x00,
//        0x48, 0x81, 0xEC, 0xA8, 0x00, 0x00, 0x00,
//        0xFF, 0xD0,
//        0x48, 0x81, 0xC4, 0xA8, 0x00, 0x00, 0x00,
//        0xC3
//    };
//
//    *(PULONG64)&bufcode[12] = msg;
//    
//    printf("Dbgview PID:%X,buff len:%x\r\n", pid, sizeof(bufcode));
//    system("pause");
//   bool b = CReadWrite.RemoteCall((HANDLE)pid, bufcode,sizeof(bufcode));
//   printf("run result:%x\r\n",b);
//   system("pause");
//}







BOOL CALLBACK DlgProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
    case WM_INITDIALOG:
        {
        SetDlgItemTextA(hwnd, IDC_EDIT_PID_FROMNAME, "123456.exe");
        SetDlgItemTextA(hwnd, IDC_EDIT_PROCESSNAME, "123456.exe");
        SetDlgItemTextA(hwnd, IDC_EDIT_DELECALLBACK_TYPE, "0");

        break;
        }
        

    case WM_COMMAND:
        switch (wParam)
        {
        case IDC_BUTTON_ProtectedPID:
            {
            wchar_t buf[0x100] = { 0 };
            GetDlgItemText(hwnd, IDC_EDIT_PID_FROMNAME, buf, 0x100);
            h.ProtectedProcess(GetProcessIdFromName(buf));
            break;
            }
            
        case IDC_BUTTON_ENUMCALLBACK:
            h.EnumCallBack();
            break;
        case IDC_BUTTON_DELCAKKBACK:
            {
            char name[0x100] = { 0 };
            GetDlgItemTextA(hwnd, IDC_EDIT_ADDRESS, name, 0x100);
            string date = name;
            DWORD64 addr =  stoull(date,nullptr,16);
            DWORD type =  GetDlgItemInt(hwnd, IDC_EDIT_DELECALLBACK_TYPE, NULL, false);
            printf("%I64X,%x\r\n", addr, type);
            h.DelteCallBack(addr, (DWORD64)type);
            break;
            }
        case IDC_BUTTON_REGOBCALLBACK:
            char name2[0x100] = { 0 };
            GetDlgItemTextA(hwnd, IDC_EDIT_PROCESSNAME, name2, 0x100);
            h.RegObCallToProtected((DWORD64)name2);
            break;
      
           
        }
        break;
    }

    return FALSE;
}

//显示资源窗口，调用此接口即可载入资源窗口
void ShowDlg()
{   //创建对话框
  //  hwndDLG = CreateDialog(hinst, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC)DlgProc);
    DialogBox(NULL, MAKEINTRESOURCE(IDD_MAIN), 0, (DLGPROC)DlgProc);
    //显示窗口
    ShowWindow(hwndDLG, SW_SHOW);

}


BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{

    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        hinst = (HINSTANCE)hModule;
        ShowDlg();
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
int main()
{
    CreateThread(0, 0, LPTHREAD_START_ROUTINE(ShowDlg), 0, 0, 0);
    system("pause");
}